home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 2.iso / STUTTGART / LANG / C / LIB / UNIXLIB37B / !UnixLib37 / src / unix / c / getenv < prev    next >
Text File  |  1996-11-09  |  4KB  |  238 lines

  1. /****************************************************************************
  2.  *
  3.  * $Source: /unixb/home/unixlib/source/unixlib37/src/unix/c/RCS/getenv,v $
  4.  * $Date: 1996/10/30 21:59:01 $
  5.  * $Revision: 1.3 $
  6.  * $State: Rel $
  7.  * $Author: unixlib $
  8.  *
  9.  * $Log: getenv,v $
  10.  * Revision 1.3  1996/10/30 21:59:01  unixlib
  11.  * Massive changes made by Nick Burret and Peter Burwood.
  12.  *
  13.  * Revision 1.2  1996/05/06 09:01:35  unixlib
  14.  * Updates to sources made by Nick Burrett, Peter Burwood and Simon Callan.
  15.  * Saved for 3.7a release.
  16.  *
  17.  * Revision 1.1  1996/04/19 21:35:27  simon
  18.  * Initial revision
  19.  *
  20.  ***************************************************************************/
  21.  
  22. static const char rcs_id[] = "$Id: getenv,v 1.3 1996/10/30 21:59:01 unixlib Rel $";
  23.  
  24. #include <stdlib.h>
  25. #include <string.h>
  26. #include <unistd.h>
  27. #include <sys/os.h>
  28. #include <sys/unix.h>
  29. #include <sys/swis.h>
  30.  
  31. /* These global variables are also initialised in unix.c.
  32.    These initialisations are for safety purposes incase one of the functions
  33.    in this file is called before unixinit.  */
  34.  
  35. char **environ = NULL;
  36.  
  37. int __envcnt = 0, __envsiz = 1;
  38.  
  39. static int
  40. __cmpenv (register char *s1, register const char *s2)
  41. {
  42.   register int i, j;
  43.  
  44.   while ((i = *s1) && i != '=' && i == *s2)
  45.     s1++, s2++;
  46.  
  47.   if (i == '=')
  48.     i = 0;
  49.   if ((j = *s2) == '=')
  50.     j = 0;
  51.  
  52.   return (i - j);
  53. }
  54.  
  55. char *
  56. __addenv (const char *s, char *v)
  57. {
  58.   register char **e, *t;
  59.   register int i, j, k;
  60.  
  61.   if (!environ)
  62.     {
  63.       /* This shouldn't be possible, unless we've been called
  64.          prior to unixinit setting environ.  */
  65.       if (!(environ = malloc (sizeof (char *))))
  66.         return NULL;
  67.       *environ = NULL;
  68.       __envcnt = 0;
  69.       __envsiz = 1;
  70.     }
  71.   i = strlen (s) + 1;
  72.   if (v)
  73.     {
  74.       j = strlen (v) + 1;
  75.       k = i + j;
  76.     }
  77.   else
  78.     {
  79.       k = i;
  80.       t = (char *) s;
  81.       while (*++t != '=');
  82.       i = t - s + 1;
  83.       j = k - i;
  84.     }
  85.  
  86.   for (e = environ; t = *e; e++)
  87.     if (!__cmpenv (t, s))
  88.       {
  89.     if (!(*e = t = realloc (t, k)))
  90.       return NULL;
  91.     goto add;
  92.       }
  93.  
  94.   if (!(t = environ[__envcnt++] = malloc (k)))
  95.     return NULL;
  96.  
  97.   if (__envcnt >= __envsiz)
  98.     {
  99.       __envsiz = (__envsiz + 64) & ~63;
  100.       if (!(environ = realloc (environ, __envsiz * sizeof (char *))))
  101.         {
  102.             /* We're in a bit of a mess now, having lost the environ space.
  103.                Just reinitialise it and return failure.  */
  104.             if (environ = malloc (sizeof (char *)))
  105.               *environ = 0;
  106.             return NULL;
  107.           }
  108.     }
  109.  
  110.   environ[__envcnt] = NULL;
  111.  
  112. add:
  113.  
  114.   if (!v)
  115.     memcpy (t, s, k);
  116.   else
  117.     {
  118.       memcpy (t, s, i);
  119.       t[i - 1] = '=';
  120.       memcpy (t + i, v, j);
  121.     }
  122.  
  123.   return (t + i);
  124. }
  125.  
  126. char *
  127. __chkenv (const char *s)
  128. {
  129.   register char **e, *t;
  130.  
  131.   if (!environ)
  132.     {
  133.       /* This shouldn't be possible, unless we've been called
  134.          prior to unixinit setting environ.  */
  135.       if (!(environ = malloc (sizeof (char *))))
  136.         return NULL;
  137.       *environ = NULL;
  138.       __envcnt = 0;
  139.       __envsiz = 1;
  140.     }
  141.  
  142.   for (e = environ; t = *e; e++)
  143.     if (!__cmpenv (t, s))
  144.       {
  145.     while (*t++ != '=');
  146.     return (t);
  147.       }
  148.  
  149.   return (0);
  150. }
  151.  
  152. int
  153. __intenv (const char *s, register int c)
  154. {
  155.   int r[10];
  156.   char buf[256];
  157.   char *b;
  158.   _kernel_oserror *e;
  159.  
  160.   if (c && (b = __chkenv (s)))
  161.     goto found;
  162.  
  163.   b = buf;
  164.   r[0] = (int) s;
  165.   r[1] = (int) b;
  166.   /* One less than buf size so can zero terminate below.  */
  167.   r[2] = sizeof (buf) - 1;
  168.   r[3] = 0;
  169.   r[4] = 3;
  170.  
  171.   if (e = os_swi (OS_ReadVarVal, r))
  172.     {
  173.       __seterr (e);
  174.       return (0);
  175.     }
  176.  
  177.   b[r[2]] = '\0';
  178.  
  179.   b = __addenv (s, b);
  180.  
  181. found:
  182. #ifdef DEBUG
  183.   os_print ("read var : ");
  184.   os_print ((char *) s);
  185.   os_print (" = ");
  186.   os_print (b);
  187.   os_print ("\r\n");
  188. #endif
  189.  
  190.   r[0] = 10;
  191.   r[1] = (int) b;
  192.  
  193.   if (e = os_swi (OS_ReadUnsigned, r))
  194.     {
  195.       __seterr (e);
  196.       return (0);
  197.     }
  198.  
  199.   return (r[2]);
  200. }
  201.  
  202. char *
  203. __getenv (const char *s, register int c)
  204. {
  205.   int r[10];
  206.   char buf[256];
  207.   char *b;
  208.   _kernel_oserror *e;
  209.  
  210.   if (c && (b = __chkenv (s)))
  211.     return (b);
  212.   else
  213.     b = buf;
  214.  
  215.   r[0] = (int) s;
  216.   r[1] = (int) b;
  217.   /* One less than buf size so can zero terminate below.  */
  218.   r[2] = sizeof (buf) - 1;
  219.   r[3] = 0;
  220.   r[4] = 3;
  221.  
  222.   if (e = os_swi (OS_ReadVarVal, r))
  223.     {
  224.       __seterr (e);
  225.       return (0);
  226.     }
  227.  
  228.   b[r[2]] = 0;
  229.  
  230.   return (__addenv (s, b));
  231. }
  232.  
  233. char *
  234. getenv (const char *s)
  235. {
  236.   return (__getenv (s, -1));
  237. }
  238.